package cn.ronghuanet.org.controller;

import cn.ronghuanet.base.util.AjaxResult;
import cn.ronghuanet.base.util.PageInfo;
import cn.ronghuanet.org.domain.Department;
import cn.ronghuanet.org.query.DepartmentQuery;
import cn.ronghuanet.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.DeclareRoles;
import java.util.List;
import java.util.Objects;

//@Controller
@Api(value = "部门管理",description = "部门操作的CRUD")
@RequestMapping("/department")
@RestController  // 该controller所有的接口均返回数据
public class DepartmentController {
    @Autowired
    private IDepartmentService service;
    /*
         达成统一:  所有的接口都应该有返回值
         并且 返回的类型 应该是一个 对象 success-boo msg-str data-Obj
         方案一 返回 map
         方案二 自定义一个 返回对象 叫做 AjaxResult(success-boo msg-str data-Obj)
         @param department
         @return
     */
    // 增/改   put

    /**
     * put 和post 请求 不能这样直接拿值 要从 requestBody 里面拿值
     * @param department
     * @return
     */
    @ApiOperation(value = "更新或新增")
    @PutMapping
    public AjaxResult save(@RequestBody  Department department){
        try {
            if (Objects.isNull(department.getId())){
                service.insert(department);
            }else{
                service.update(department);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }

    // 删 delete
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        // 有返回值
        try {
            service.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    // 查全部 get
    @GetMapping
    public AjaxResult selectList(){
        // 有返回值
        try {
            List<Department> departments = service.selectAll();
            return AjaxResult.success(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }


    }

    // 查一个 get
    @GetMapping("/{id}")
    public AjaxResult selectByID(@PathVariable("id") Long id){
        try {
            Department department = service.selectById(id);
            return AjaxResult.success(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }


    // 分页查询 post
    @PostMapping
    public AjaxResult queryPage(@RequestBody DepartmentQuery departmentQuery){
        try {
            PageInfo pageInfo = service.queryPage(departmentQuery);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    // 批量删除 delete
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        // 有返回值
        try {
            service.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    // 查一个 get
    @GetMapping("/deptTree")
    public AjaxResult getDeptTree(){
        try {
            List<Department> department = service.getDeptTree();
            return AjaxResult.success(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

}
